package com.report.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.operator.Contain;
import cn.zhxu.bs.operator.Equal;
import cn.zhxu.bs.util.MapUtils;
import com.report.dto.request.InsightReq;
import com.report.dto.response.InsightRes;
import com.report.dto.response.InsightWebRes;
import com.report.dto.response.ReportRes;
import com.report.entity.Insight;
import com.report.entity.Report;
import com.report.mapper.InsightMapper;
import com.report.service.IReportService;
import com.report.service.InsightService;
import com.zmh.framework.constants.Constants;
import com.zmh.framework.oss.OssConfigure;
import com.zmh.framework.sys.dto.request.common.PageResponseDTO;
import com.zmh.framework.util.Base64ToMultipartFile;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author summer
 * @since 2024/06/27 16:24:42
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class InsightServiceImpl extends ServiceImpl<InsightMapper, Insight> implements InsightService {

    private final BeanSearcher beanSearcher;

    private final OssConfigure ossConfigure;

    private final IReportService reportService;

    @Override
    public void saveInsight(InsightReq insightReq) {
         if(insightReq.getShowImage()){
            MultipartFile multipartFile = Base64ToMultipartFile.base64ToMultipart(insightReq.getImage());
            String s = ossConfigure.uploadFile(multipartFile);
            insightReq.setImage(ossConfigure.getAccessUrl()+s);
        }

        this.saveOrUpdate(Convert.convert(Insight.class, insightReq));
    }

    @Override
    public Page<InsightRes> lists(InsightReq insightReq) {
        Page<InsightRes> page = new Page<>(insightReq.getPageNum(), insightReq.getPageSize());
        Map<String, Object> build = MapUtils.builder().page(insightReq.getPageNum() - 1, insightReq.getPageSize())
                .field(InsightRes::getName, insightReq.getName()).op(Contain.class)
                .field(InsightRes::getDeleted,Constants.ZERO).op(Equal.class)
                .build()
                ;
        SearchResult<InsightRes> search = beanSearcher.search(InsightRes.class, build);
        page.setTotal(search.getTotalCount().longValue());
        page.setRecords(search.getDataList());
        return page;
    }

    @Override
    public InsightRes findInsightById(Integer id) {
        Map<String, Object> build = MapUtils.builder()
                .field(InsightRes::getId, id).op(Equal.class).build();
        return beanSearcher.searchFirst(InsightRes.class, build);
    }

    @Override
    public Page<InsightRes> insightList(InsightReq insightReq) {
        Page<Insight> page=new Page<>(insightReq.getPageNum(), insightReq.getPageSize());
        LambdaQueryWrapper<Insight> insightLambdaQueryWrapper = Wrappers.lambdaQuery(Insight.class).
                select(Insight::getReportId,Insight::getBrowseNum,Insight::getId,Insight::getCreateTime,Insight::getImage,Insight::getName,Insight::getId,Insight::getContext).
                orderByDesc(Insight::getCreateTime);
        page = this.page(page,insightLambdaQueryWrapper);
        return Convert.convert(new TypeReference<Page<InsightRes>>() {},page);
    }

    @Override
    public List<InsightWebRes> preNextInsight(Integer id) {
        List<InsightWebRes> list= ListUtil.list(true);
        InsightWebRes pre=this.baseMapper.findPreInsight(id);
        if(ObjectUtil.isNotNull(pre)){
            list.add(pre);
        }
        InsightWebRes next=this.baseMapper.findNextInsight(id);
        if(ObjectUtil.isNotNull(next)){
            list.add(next);
        }
        return list;
    }

    @Override
    public InsightRes findById(Integer id) {
         Map<String, Object> build = MapUtils.builder()
                .field(InsightRes::getId, id).op(Equal.class).build();
        return beanSearcher.searchFirst(InsightRes.class, build);
    }

    @Override
    public Page<InsightRes> insightHotspot(PageResponseDTO pageResponseDTO) {
        Page<Insight> page = new Page<>(pageResponseDTO.getPageNum(), pageResponseDTO.getPageSize());
        LambdaQueryWrapper<Insight> eq = Wrappers.lambdaQuery(Insight.class).eq(Insight::getHotspot, Constants.ONE);
        return Convert.convert(new TypeReference<Page<InsightRes>>() {
        },this.page(page,eq));
    }

    @Override
    public void saveReadInsight(Integer id) {
        Insight insight = this.getById(id);
        insight.setBrowseNum(insight.getBrowseNum()+1);
        this.updateById(insight);
    }
}
